using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_misc_Header : System.Web.UI.UserControl
{
    public string ItemText
    {
        get
        {
            return ltrItem.Text;
        }
        set
        {
            ltrItem.Text = value;
        }
    }

    public string SubTitle
    {
        get
        {
            return ltrSub.Text;
        }
        set
        {
            ltrSub.Text = value;
        }
    }

    public void SetItem(DAL.ForumCategory category)
    {
        SetItem(Helpers.StringHelper.SafeFormat(category.Name));
        SetList();
    }

    public void SetItem(DAL.Forum forum)
    {
        SetItem(Helpers.StringHelper.SafeFormat(forum.Name),
            Helpers.StringHelper.SafeFormat(forum.Description));
        SetList(forum, true);
    }

    public void SetItem(DAL.Thread thread)
    {
        SetItem(Helpers.StringHelper.SafeFormat(thread.FirstPost.Subject));
        SetList(thread.Forum, false);
    }

    public void SetItem(DAL.Post post)
    {
        SetItem(Helpers.StringHelper.SafeFormat(post.Subject));
        SetList(post.Thread);
    }

    public void SetItem(DAL.Album album)
    {
        List<HyperLink> links = GetItemList();
        HyperLink photolink = new HyperLink();
        photolink.Text = Helpers.StringHelper.SafeFormat("{0}'s Photos", album.User.Username);
        photolink.NavigateUrl = "~/forums/photos.aspx";
        links.Add(photolink);
        SetDataSource(links);
        SetItem(Helpers.StringHelper.SafeFormat(album.Name));
    }

    public void SetItem(string value, string subtitle)
    {
        ItemText = value;
        SubTitle = subtitle;
    }

    public void SetItem(string value)
    {
        SetItem(value, "");
    }

    public void SetList()
    {
        List<HyperLink> items = GetItemList();
        SetDataSource(items);
    }

    public void SetList(DAL.Forum forum, bool startWithParent)
    {
        List<HyperLink> items = GetItemList();
        items.Add(GetHyperlink(forum.Category));
        if (startWithParent)
        {
            items.AddRange(GetForumLinks(forum.Parent));
        }
        else
        {
            items.AddRange(GetForumLinks(forum));
        }

        SetDataSource(items);
    }

    public void SetList(DAL.Thread thread)
    {
        List<HyperLink> items = GetItemList();
        items.Add(GetHyperlink(thread.Forum.Category));
        items.AddRange(GetForumLinks(thread.Forum));
        SetDataSource(items);
    }

    public List<HyperLink> GetItemList()
    {
        List<HyperLink> items = new List<HyperLink>(10);
        items.Add(GetHyperlink("Awesome Forums", "~/forums/default.aspx"));
        return items;
    }

    private Stack<HyperLink> GetForumLinks(DAL.Forum rootForum)
    {
        Stack<HyperLink> links = new Stack<HyperLink>();
        while (rootForum != null)
        {
            links.Push(GetHyperlink(rootForum));
            rootForum = rootForum.Parent;
        }

        return links;
    }

    private HyperLink GetHyperlink(string text, string url)
    {
        HyperLink link = new HyperLink();
        link.Text = Helpers.StringHelper.SafeFormat(text);
        link.NavigateUrl = url;
        return link;
    }

    private HyperLink GetHyperlink(DAL.Forum forum)
    {
        return GetHyperlink(forum.Name, "~/forums/forum.aspx?f=" + forum.ID.ToString());
    }

    private HyperLink GetHyperlink(DAL.ForumCategory category)
    {
        return GetHyperlink(category.Name, "~/forums/category.aspx?c=" + category.ID.ToString());
    }

    private HyperLink GetHyperlink(DAL.Thread thread)
    {
        return GetHyperlink(thread.FirstPost.Subject, "~/forums/thread.aspx?t=" + thread.ID.ToString());
    }

    private void SetDataSource(List<HyperLink> items)
    {
        rptBreadcrumb.DataSource = items;
        rptBreadcrumb.DataBind();
    }
}